<?php
class Dataset_db extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function getNav() {
        $user = $this->session->userdata("user");
        $this->db->select('mi.miid, mi.mgid, mi.milabel, mg.mglabel, mg.mgposition, mi.miposition, mg.mgurl, mi.miurl, mi.mihidden');
        $this->db->from('roleaccess ra');
        $this->db->join('menuitem mi','ra.miid = mi.miid','left');
        $this->db->join('menugroup mg','mi.mgid = mg.mgid','left');
        $this->db->where('ra.roleid',$user['roleid']);
        $this->db->where('ra.allowed','1');
        $this->db->where('mi.miactive','1');
        //$this->db->where('mi.mihidden','0');
        $this->db->order_by("mg.mgposition", "asc");
        $this->db->order_by("mg.mgid", "asc");
        $this->db->order_by("mi.miposition", "asc");
        $query = $this->db->get();
        //print_object($this->db->last_query());exit;
        return $query->result_array();
    }
    
    function get_access_data() {
        $user = $this->session->userdata("user");
        $this->db->select('mi.miid, mi.mgid, mi.milabel, mg.mglabel, mg.mgposition, mi.miposition, mg.mgurl, mi.miurl');
        $this->db->from('roleaccess ra');
        $this->db->join('menuitem mi','ra.miid = mi.miid','left');
        $this->db->join('menugroup mg','mi.mgid = mg.mgid','left');
        $this->db->where('ra.roleid',$user['roleid']);
        $this->db->where('ra.allowed','1');
        $this->db->order_by("mg.mgposition", "asc");
        $this->db->order_by("mi.miposition", "asc"); 
        $query = $this->db->get();
        //print_object($this->db->last_query());
        return $query->result_array();
    }
    
    function check_access($classname, $action){
        $user = $this->session->userdata("user");
        $this->db->from('roleaccess ra');
        $this->db->join('menuitem mi','ra.miid = mi.miid','left');
        $this->db->join('menugroup mg','mi.mgid = mg.mgid','left');
        $this->db->where('ra.roleid',$user['roleid']);
        $this->db->where('ra.allowed','1');
        $this->db->where('mg.mgurl',$classname);
        $this->db->where('mi.miurl',$action);
        $this->db->where('mi.miactive','1');
        $num_rows = $this->db->count_all_results();
        if($num_rows>0){
            return true;
        } else {
            return false;
        }
    }
    
    function build_access_map() {
        $rs = $this->get_access_data();
        $access_map = array();
        foreach ($rs as $row) {
            $url = $row['mgurl'].'/'.$row['miurl'];
            $access_map[$url] = $row['miid'];
        }
        return $access_map;
    }
	
	function buildNav()
	{
		$rs = $this->getNav();
        //print_object($rs);exit; 
        $curGroup = "";
    	$prevGroup = "";
        $result = array();
        $links = array();
        $selected = array("group" => "", "item" => "");

		foreach ($rs as $row) {
			if( $row['mgid'] == "" )
                continue;
            $curGroup = $row['mgid'];
            
            if($curGroup != $prevGroup)	{
                $group = array();
                $group['id'] = $row['mgid'];
                $group['label'] = $row['mglabel'];
                $group['position'] = $row['mgposition'];
                $group['url'] = $row['mgurl'];
                $group['cname'] = $row['mgurl'];
                $group['items'] = array();
                
                if($row['mgurl'] == $this->router->class) {
                    $group['selected'] = true;
                    $selected['group'] = $row['mglabel'];
                } else {
                    $group['selected'] = false;
                }
                
                $links[$row['mgid']] = $group;
                
            	$prevGroup = $curGroup;
            }
            
            $link = array();
            $link['id'] = $row['miid'];
            $link['label'] = $row['milabel'];
            $link['position'] = $row['miposition'];
            $link['url'] = $row['miurl'];
            
            if($row['miurl'] == $this->router->method) {
                    $link['selected'] = true;
                    $selected['item'] = $row['milabel'];
                    
            } else {
                    $link['selected'] = false;
            }
            if($row['mihidden']==0)
                array_push($links[$row['mgid']]['items'], $link);		
		}
        $result['links'] = $links;
        $result['selected'] = $selected;
        //print_object($result);exit;     				
		return $result;
	}
	
	function get_countries()	{
		$this->db->order_by("name", "asc"); 
		$query = $this->db->get("country");
		$result = $query->result_array();
        return $result; 		
	}
    
    function get_maritalstatus() {
        $query = $this->db->get('maritalstatus');
        $result = $query->result_array();
        return $result;
    }
    
    function save_temp_data($datakey, $datavalue) {
        $user = $this->session->userdata('user');
        $userid = $user['userid'];
        $this->db->delete('tempdata', array('user_id' => $userid, 'data_key' => $datakey));
        $data = array(
           'user_id' => $userid ,
           'data_key' => $datakey ,
           'data_value' => $datavalue
        );
        
        $this->db->insert('tempdata', $data);     
    }
    
    function get_temp_data($datakey) {
        $user = $this->session->userdata('user');
        $userid = $user['userid'];
        $this->db->select('data_value');
        $this->db->where('user_id', $userid);
        $this->db->where('data_key', $datakey);
        
        $query = $this->db->get('tempdata');
        $result = $query->result_array();
        return (count($result) >0) ? $result[0]['data_value'] : '';
    }
    
    function delete_temp_data() {
        $user = $this->session->userdata('user');
        $userid = $user['userid'];
        $this->db->delete('tempdata', array('user_id' => $userid));
    }

}

?>